{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=workshops/YouthMappers_2021.ipynb)\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://gishub.org/ym-colab)\n",
    "[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/ym-binder)\n",
    "[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/ym-binder-nb)\n",
    "![](https://i.imgur.com/5FhzCc0.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "**Interactive Mapping and Geospatial Analysis with Leafmap and Jupyter**\n",
    "\n",
    "\n",
    "This notebook was developed for the 90-min [leafmap workshop](https://www.eventbrite.com/e/interactive-mapping-and-geospatial-analysis-tickets-188600217327?keep_tld=1) taking place on November 9, 2021. The workshop is hosted by [YouthMappers](https://www.youthmappers.org).\n",
    "\n",
    "- Author: [Qiusheng Wu](https://github.com/opengeos)\n",
    "- Slides: https://gishub.org/ym \n",
    "- Streamlit web app: https://streamlit.gishub.org\n",
    "\n",
    "Launch this notebook to execute code interactively using: \n",
    "- Google Colab: https://gishub.org/ym-colab\n",
    "- Pangeo Binder JupyterLab: https://gishub.org/ym-binder\n",
    "- Pangeo Binder Jupyter Notebook: https://gishub.org/ym-binder-nb\n",
    "\n",
    "## Introduction\n",
    "\n",
    "### Workshop description\n",
    "\n",
    "[Leafmap](https://leafmap.org) is a Python package for interactive mapping and geospatial analysis with minimal coding in a Jupyter environment. It is built upon a number of open-source packages, such as [folium](https://github.com/python-visualization/folium) and [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) (for creating interactive maps), [WhiteboxTools](https://github.com/jblindsay/whitebox-tools) and [whiteboxgui](https://github.com/opengeos/whiteboxgui) (for analyzing geospatial data), and [ipywidgets](https://github.com/jupyter-widgets/ipywidgets) (for designing interactive graphical user interface). The WhiteboxTools library currently contains 480+ tools for advanced geospatial analysis. Leafmap provides many convenient functions for loading and visualizing geospatial data with only one line of code. Users can also use the interactive user interface to load geospatial data without coding. Anyone with a web browser and Internet connection can use leafmap to perform geospatial analysis and data visualization in the cloud with minimal coding. The topics that will be covered in this workshop include: \n",
    "\n",
    "- A brief introduction to Jupyter and Colab\n",
    "- A brief introduction to leafmap and relevant web resources \n",
    "- Creating interactive maps using multiple plotting backends\n",
    "- Searching and loading basemaps\n",
    "- Loading and visualizing vector/raster data\n",
    "- Using Cloud Optimized GeoTIFF (COG) and SpatialTemporal Asset Catalog (STAC)\n",
    "- Downloading OpenStreetMap data\n",
    "- Loading data from a PostGIS database\n",
    "- Creating custom legends and colorbars\n",
    "- Creating split-panel maps and linked maps\n",
    "- Visualizing Planet global monthly/quarterly mosaic\n",
    "- Designing and publishing interactive web apps\n",
    "- Performing geospatial analysis (e.g., hydrological analysis) using whiteboxgui\n",
    "\n",
    "This workshop is intended for scientific programmers, data scientists, geospatial analysts, and concerned citizens of Earth. The attendees are expected to have a basic understanding of Python and the Jupyter ecosystem. Familiarity with Earth science and geospatial datasets is useful but not required. More information about leafmap can be found at https://leafmap.org.\n",
    "\n",
    "\n",
    "### Jupyter keyboard shortcuts\n",
    "\n",
    "- Shift+Enter: run cell, select below\n",
    "- Ctrl+Enter: : run selected cells\n",
    "- Alt+Enter: run cell and insert below\n",
    "- Tab: code completion or indent\n",
    "- Shift+Tab: tooltip\n",
    "- Ctrl+/: comment out code"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## Set up environment\n",
    "\n",
    "### Required Python packages:\n",
    "* [leafmap](https://github.com/opengeos/leafmap) - A Python package for interactive mapping and geospatial analysis with minimal coding in a Jupyter environment.\n",
    "* [keplergl](https://docs.kepler.gl/docs/keplergl-jupyter) - A high-performance web-based application for visual exploration of large-scale geolocation data sets.\n",
    "* [pydeck](https://deckgl.readthedocs.io/en/latest) - High-scale spatial rendering in Python, powered by deck.gl.\n",
    "* [geopandas](https://geopandas.org) - An open source project to make working with geospatial data in python easier. \n",
    "* [xarray-leaflet](https://github.com/davidbrochart/xarray_leaflet) - An xarray extension for tiled map plotting.\n",
    "\n",
    "### Use Google Colab\n",
    "\n",
    "Click the button below to open this notebook in Google Colab and execute code interactively.\n",
    "\n",
    "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=workshops/YouthMappers_2021.ipynb)\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/workshops/YouthMappers_2021.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import subprocess\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "A function for installing Python packages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def install(package):\n",
    "    subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", package])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "Install required Python packages in Google Colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "pkgs = [\n",
    "    \"leafmap\",\n",
    "    \"geopandas\",\n",
    "    \"keplergl\",\n",
    "    \"pydeck\",\n",
    "    \"xarray_leaflet\",\n",
    "    \"osmnx\",\n",
    "    \"pygeos\",\n",
    "    \"imageio\",\n",
    "    \"tifffile\",\n",
    "]\n",
    "if \"google.colab\" in sys.modules:\n",
    "    for pkg in pkgs:\n",
    "        install(pkg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "### Use Pangeo Binder\n",
    "\n",
    "Click the buttons below to open this notebook in JupyterLab (first button) or Jupyter Notebook (second button) and execute code interactively.\n",
    "\n",
    "[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/ym-binder)\n",
    "[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/ym-binder-nb)\n",
    "\n",
    "- JupyterLab: https://gishub.org/ym-binder\n",
    "- Jupyter Notebook: https://gishub.org/ym-binder-nb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "### Use Miniconda/Anaconda\n",
    "\n",
    "If you have\n",
    "[Anaconda](https://www.anaconda.com/distribution/#download-section) or [Miniconda](https://docs.conda.io/en/latest/miniconda.html) installed on your computer, you can install leafmap using the following commands. Leafmap has an optional dependency - [geopandas](https://geopandas.org), which can be challenging to install on some computers, especially Windows. It is highly recommended that you create a fresh conda environment to install geopandas and leafmap. Follow the commands below to set up a conda env and install geopandas, leafmap, pydeck, keplergl, and xarray_leaflet. \n",
    "\n",
    "```\n",
    "conda create -n geo python=3.8\n",
    "conda activate geo\n",
    "conda install geopandas\n",
    "conda install mamba -c conda-forge\n",
    "mamba install leafmap keplergl pydeck xarray_leaflet -c conda-forge\n",
    "mamba install osmnx pygeos imageio tifffile -c conda-forge\n",
    "jupyter lab\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    import leafmap\n",
    "except ImportError:\n",
    "    install(\"leafmap\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "## Create an interactive map\n",
    "\n",
    "`Leafmap` has five plotting backends: [folium](https://github.com/python-visualization/folium), [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet), [here-map](https://github.com/heremaps/here-map-widget-for-jupyter), [kepler.gl](https://docs.kepler.gl/docs/keplergl-jupyter), and [pydeck](https://deckgl.readthedocs.io). Note that the backends do not offer equal functionality. Some interactive functionality in `ipyleaflet` might not be available in other plotting backends. To use a specific plotting backend, use one of the following:\n",
    "\n",
    "- `import leafmap.leafmap as leafmap`\n",
    "- `import leafmap.foliumap as leafmap`\n",
    "- `import leafmap.heremap as leafmap`\n",
    "- `import leafmap.kepler as leafmap`\n",
    "- `import leafmap.deck as leafmap`\n",
    "\n",
    "### Use ipyleaflet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap\n",
    "\n",
    "m = leafmap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "### Use folium"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap.foliumap as leafmap\n",
    "\n",
    "m = leafmap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "### Use kepler.gl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap.kepler as leafmap\n",
    "\n",
    "m = leafmap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "If you encounter an error saying `Error displaying widget: model not found` when trying to display the map, you can use `m.static_map()` as a workaround until this [kepler.gl bug](https://github.com/keplergl/kepler.gl/issues/1165) has been resolved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# m.static_map(width=1280, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21",
   "metadata": {},
   "source": [
    "### Use pydeck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap.deck as leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "## Customize the default map\n",
    "\n",
    "### Specify map center and zoom level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=(40, -100), zoom=4)  # center=[lat, lon]\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=(51.5, -0.15), zoom=17)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28",
   "metadata": {},
   "source": [
    "### Change map size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(height=\"400px\", width=\"800px\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30",
   "metadata": {},
   "source": [
    "### Set control visibility\n",
    "\n",
    "When creating a map, set the following controls to either `True` or `False` as appropriate.\n",
    "\n",
    "* attribution_control\n",
    "* draw_control\n",
    "* fullscreen_control\n",
    "* layers_control\n",
    "* measure_control\n",
    "* scale_control\n",
    "* toolbar_control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(\n",
    "    draw_control=False,\n",
    "    measure_control=False,\n",
    "    fullscreen_control=False,\n",
    "    attribution_control=False,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32",
   "metadata": {},
   "source": [
    "Remove all controls from the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.clear()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34",
   "metadata": {},
   "source": [
    "## Change basemaps\n",
    "\n",
    "Specify a Google basemap to use, can be one of [\"ROADMAP\", \"TERRAIN\", \"SATELLITE\", \"HYBRID\"]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(google_map=\"TERRAIN\")  # HYBRID, ROADMAP, SATELLITE, TERRAIN\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37",
   "metadata": {},
   "source": [
    "Add a basemap using the `add_basemap()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_basemap(\"Esri.NatGeoWorldMap\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39",
   "metadata": {},
   "source": [
    "Print out the list of available basemaps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40",
   "metadata": {},
   "outputs": [],
   "source": [
    "for basemap in leafmap.basemaps:\n",
    "    print(basemap)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/T1oBWSz.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_tile_layer(\n",
    "    url=\"https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}\",\n",
    "    name=\"Google Satellite\",\n",
    "    attribution=\"Google\",\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43",
   "metadata": {},
   "source": [
    "## Add tile layers\n",
    "\n",
    "### Add XYZ tile layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_tile_layer(\n",
    "    url=\"https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}\",\n",
    "    name=\"Google Satellite\",\n",
    "    attribution=\"Google\",\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46",
   "metadata": {},
   "source": [
    "### Add WMS tile layer\n",
    "\n",
    "More WMS basemaps can be found at the following websites:\n",
    "\n",
    "- USGS National Map: https://viewer.nationalmap.gov/services\n",
    "- MRLC NLCD Land Cover data: https://www.mrlc.gov/data-services-page\n",
    "- FWS NWI Wetlands data: https://www.fws.gov/wetlands/Data/Web-Map-Services.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "naip_url = \"https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?\"\n",
    "m.add_wms_layer(\n",
    "    url=naip_url, layers=\"0\", name=\"NAIP Imagery\", format=\"image/png\", shown=True\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48",
   "metadata": {},
   "source": [
    "### Add xyzservices provider\n",
    "\n",
    "Add a layer from [xyzservices](https://github.com/geopandas/xyzservices) provider object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import xyzservices.providers as xyz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50",
   "metadata": {},
   "outputs": [],
   "source": [
    "basemap = xyz.OpenTopoMap\n",
    "basemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_basemap(basemap)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52",
   "metadata": {},
   "source": [
    "## Add COG/STAC layers\n",
    "\n",
    "A Cloud Optimized GeoTIFF (COG) is a regular GeoTIFF file, aimed at being hosted on a HTTP file server, with an internal organization that enables more efficient workflows on the cloud. It does this by leveraging the ability of clients issuing HTTP GET range requests to ask for just the parts of a file they need. \n",
    "\n",
    "More information about COG can be found at <https://www.cogeo.org/in-depth.html>\n",
    "\n",
    "Some publicly available Cloud Optimized GeoTIFFs:\n",
    "\n",
    "* https://stacindex.org/\n",
    "* https://cloud.google.com/storage/docs/public-datasets/landsat\n",
    "* https://www.digitalglobe.com/ecosystem/open-data\n",
    "* https://earthexplorer.usgs.gov/\n",
    "\n",
    "For this demo, we will use data from https://www.maxar.com/open-data/california-colorado-fires for mapping California and Colorado fires. A list of COGs can be found [here](https://github.com/opengeos/leafmap/blob/master/examples/data/cog_files.txt).\n",
    "\n",
    "### Add COG layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "url = \"https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif\"\n",
    "url2 = \"https://github.com/opengeos/data/releases/download/raster/Libya-2023-09-13.tif\"\n",
    "\n",
    "m.add_cog_layer(url, name=\"Fire (pre-event)\")\n",
    "m.add_cog_layer(url2, name=\"Fire (post-event)\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55",
   "metadata": {},
   "source": [
    "### Add STAC layer\n",
    "\n",
    "The SpatioTemporal Asset Catalog (STAC) specification provides a common language to describe a range of geospatial information, so it can more easily be indexed and discovered. A 'spatiotemporal asset' is any file that represents information about the earth captured in a certain space and time. The initial focus is primarily remotely-sensed imagery (from satellites, but also planes, drones, balloons, etc), but the core is designed to be extensible to SAR, full motion video, point clouds, hyperspectral, LiDAR and derived data like NDVI, Digital Elevation Models, mosaics, etc. More information about STAC can be found at https://stacspec.org/\n",
    "\n",
    "Some publicly available SpatioTemporal Asset Catalog (STAC):\n",
    "\n",
    "* https://stacindex.org\n",
    "\n",
    "For this demo, we will use STAC assets from https://stacindex.org/catalogs/spot-orthoimages-canada-2005#/?t=catalogs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "url = \"https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json\"\n",
    "m.add_stac_layer(url, bands=[\"B3\", \"B2\", \"B1\"], name=\"False color\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57",
   "metadata": {},
   "source": [
    "## Add local raster datasets\n",
    "\n",
    "The `add_raster` function relies on the `xarray_leaflet` package and is only available for the ipyleaflet plotting backend. Therefore, Google Colab is not supported. Note that `xarray_leaflet` does not work properly on Windows ([source](https://github.com/davidbrochart/xarray_leaflet/issues/30))."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import leafmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59",
   "metadata": {},
   "source": [
    "Download samples raster datasets. More datasets can be downloaded from https://viewer.nationalmap.gov/basic/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.getcwd()\n",
    "\n",
    "landsat = os.path.join(out_dir, \"landsat.tif\")\n",
    "dem = os.path.join(out_dir, \"dem.tif\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61",
   "metadata": {},
   "source": [
    "Download a small Landsat imagery."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62",
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat_url = (\n",
    "    \"https://drive.google.com/file/d/1EV38RjNxdwEozjc9m0FcO3LFgAoAX1Uw/view?usp=sharing\"\n",
    ")\n",
    "leafmap.download_file(landsat_url, \"landsat.tif\", unzip=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63",
   "metadata": {},
   "source": [
    "Download a small DEM dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64",
   "metadata": {},
   "outputs": [],
   "source": [
    "dem_url = (\n",
    "    \"https://drive.google.com/file/d/1vRkAWQYsLWCi6vcTMk8vLxoXMFbdMFn8/view?usp=sharing\"\n",
    ")\n",
    "leafmap.download_file(dem_url, \"dem.tif\", unzip=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66",
   "metadata": {},
   "source": [
    "Add local raster datasets to the map\n",
    "\n",
    "More colormap can be found at https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.add_raster(dem, colormap=\"terrain\", layer_name=\"DEM\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.add_raster(landsat, bands=[5, 4, 3], layer_name=\"Landsat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69",
   "metadata": {},
   "outputs": [],
   "source": [
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70",
   "metadata": {},
   "source": [
    "## Add legend\n",
    "\n",
    "### Add built-in legend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72",
   "metadata": {},
   "source": [
    "List all available built-in legends."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73",
   "metadata": {},
   "outputs": [],
   "source": [
    "legends = leafmap.builtin_legends\n",
    "for legend in legends:\n",
    "    print(legend)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74",
   "metadata": {},
   "source": [
    "Add a WMS layer and built-in legend to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "url = \"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?\"\n",
    "m.add_wms_layer(\n",
    "    url,\n",
    "    layers=\"NLCD_2019_Land_Cover_L48\",\n",
    "    name=\"NLCD 2019 CONUS Land Cover\",\n",
    "    format=\"image/png\",\n",
    "    transparent=True,\n",
    ")\n",
    "m.add_legend(builtin_legend=\"NLCD\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76",
   "metadata": {},
   "source": [
    "Add U.S. National Wetlands Inventory (NWI). More info at https://www.fws.gov/wetlands."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(google_map=\"HYBRID\")\n",
    "\n",
    "url1 = \"https://www.fws.gov/wetlands/arcgis/services/Wetlands/MapServer/WMSServer?\"\n",
    "m.add_wms_layer(\n",
    "    url1, layers=\"1\", format=\"image/png\", transparent=True, name=\"NWI Wetlands Vector\"\n",
    ")\n",
    "\n",
    "url2 = \"https://www.fws.gov/wetlands/arcgis/services/Wetlands_Raster/ImageServer/WMSServer?\"\n",
    "m.add_wms_layer(\n",
    "    url2, layers=\"0\", format=\"image/png\", transparent=True, name=\"NWI Wetlands Raster\"\n",
    ")\n",
    "\n",
    "m.add_legend(builtin_legend=\"NWI\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78",
   "metadata": {},
   "source": [
    "### Add custom legend\n",
    "\n",
    "There are two ways you can add custom legends:\n",
    "\n",
    "1. Define legend labels and colors\n",
    "2. Define legend dictionary\n",
    "\n",
    "Define legend keys and colors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "\n",
    "labels = [\"One\", \"Two\", \"Three\", \"Four\", \"etc\"]\n",
    "# color can be defined using either hex code or RGB (0-255, 0-255, 0-255)\n",
    "colors = [\"#8DD3C7\", \"#FFFFB3\", \"#BEBADA\", \"#FB8072\", \"#80B1D3\"]\n",
    "# colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68, 123)]\n",
    "\n",
    "m.add_legend(title=\"Legend\", labels=labels, colors=colors)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80",
   "metadata": {},
   "source": [
    "Define a legend dictionary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "\n",
    "url = \"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?\"\n",
    "m.add_wms_layer(\n",
    "    url,\n",
    "    layers=\"NLCD_2019_Land_Cover_L48\",\n",
    "    name=\"NLCD 2019 CONUS Land Cover\",\n",
    "    format=\"image/png\",\n",
    "    transparent=True,\n",
    ")\n",
    "\n",
    "legend_dict = {\n",
    "    \"11 Open Water\": \"466b9f\",\n",
    "    \"12 Perennial Ice/Snow\": \"d1def8\",\n",
    "    \"21 Developed, Open Space\": \"dec5c5\",\n",
    "    \"22 Developed, Low Intensity\": \"d99282\",\n",
    "    \"23 Developed, Medium Intensity\": \"eb0000\",\n",
    "    \"24 Developed High Intensity\": \"ab0000\",\n",
    "    \"31 Barren Land (Rock/Sand/Clay)\": \"b3ac9f\",\n",
    "    \"41 Deciduous Forest\": \"68ab5f\",\n",
    "    \"42 Evergreen Forest\": \"1c5f2c\",\n",
    "    \"43 Mixed Forest\": \"b5c58f\",\n",
    "    \"51 Dwarf Scrub\": \"af963c\",\n",
    "    \"52 Shrub/Scrub\": \"ccb879\",\n",
    "    \"71 Grassland/Herbaceous\": \"dfdfc2\",\n",
    "    \"72 Sedge/Herbaceous\": \"d1d182\",\n",
    "    \"73 Lichens\": \"a3cc51\",\n",
    "    \"74 Moss\": \"82ba9e\",\n",
    "    \"81 Pasture/Hay\": \"dcd939\",\n",
    "    \"82 Cultivated Crops\": \"ab6c28\",\n",
    "    \"90 Woody Wetlands\": \"b8d9eb\",\n",
    "    \"95 Emergent Herbaceous Wetlands\": \"6c9fb8\",\n",
    "}\n",
    "\n",
    "m.add_legend(title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82",
   "metadata": {},
   "source": [
    "## Add colormap\n",
    "\n",
    "The colormap functionality requires the ipyleaflet plotting backend. Folium is not supported."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap\n",
    "import leafmap.colormaps as cm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84",
   "metadata": {},
   "source": [
    "### Common colormaps\n",
    "\n",
    "Color palette for DEM data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.palettes.dem"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86",
   "metadata": {},
   "source": [
    "Show the DEM palette."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormap(colors=cm.palettes.dem, axis_off=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88",
   "metadata": {},
   "source": [
    "Color palette for NDVI data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.palettes.ndvi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90",
   "metadata": {},
   "source": [
    "Show the NDVI palette."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormap(colors=cm.palettes.ndvi)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92",
   "metadata": {},
   "source": [
    "### Custom colormaps\n",
    "\n",
    "Specify the number of classes for a palette."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.get_palette(\"terrain\", n_class=8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94",
   "metadata": {},
   "source": [
    "Show the terrain palette with 8 classes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormap(colors=cm.get_palette(\"terrain\", n_class=8))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96",
   "metadata": {},
   "source": [
    "Create a palette with custom colors, label, and font size."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormap(colors=[\"red\", \"green\", \"blue\"], label=\"Temperature\", font_size=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98",
   "metadata": {},
   "source": [
    "Create a discrete color palette."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormap(\n",
    "    colors=[\"red\", \"green\", \"blue\"], discrete=True, label=\"Temperature\", font_size=12\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "100",
   "metadata": {},
   "source": [
    "Specify the width and height for the palette."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "101",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormap(\n",
    "    \"terrain\",\n",
    "    label=\"Elevation\",\n",
    "    width=8.0,\n",
    "    height=0.4,\n",
    "    orientation=\"horizontal\",\n",
    "    vmin=0,\n",
    "    vmax=1000,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "102",
   "metadata": {},
   "source": [
    "Change the orentation of the colormap to be vertical."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "103",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormap(\n",
    "    \"terrain\",\n",
    "    label=\"Elevation\",\n",
    "    width=0.4,\n",
    "    height=4,\n",
    "    orientation=\"vertical\",\n",
    "    vmin=0,\n",
    "    vmax=1000,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "104",
   "metadata": {},
   "source": [
    "### Horizontal colormap\n",
    "\n",
    "Add a horizontal colorbar to an interactive map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "105",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_basemap(\"OpenTopoMap\")\n",
    "m.add_colormap(\n",
    "    \"terrain\",\n",
    "    label=\"Elevation\",\n",
    "    width=8.0,\n",
    "    height=0.4,\n",
    "    orientation=\"horizontal\",\n",
    "    vmin=0,\n",
    "    vmax=4000,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "106",
   "metadata": {},
   "source": [
    "### Vertical colormap\n",
    "\n",
    "Add a vertical colorbar to an interactive map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "107",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_basemap(\"OpenTopoMap\")\n",
    "m.add_colormap(\n",
    "    \"terrain\",\n",
    "    label=\"Elevation\",\n",
    "    width=0.4,\n",
    "    height=4,\n",
    "    orientation=\"vertical\",\n",
    "    vmin=0,\n",
    "    vmax=4000,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "108",
   "metadata": {},
   "source": [
    "### List of available colormaps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "109",
   "metadata": {},
   "outputs": [],
   "source": [
    "cm.plot_colormaps(width=12, height=0.4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "110",
   "metadata": {},
   "source": [
    "## Add vector datasets\n",
    "\n",
    "### Add CSV\n",
    "\n",
    "Read a CSV as a Pandas DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "111",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "112",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_csv = \"https://raw.githubusercontent.com/opengeos/data/main/world/world_cities.csv\"\n",
    "df = leafmap.csv_to_df(in_csv)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "113",
   "metadata": {},
   "source": [
    "Create a point layer from a CSV file containing lat/long information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "114",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_xy_data(in_csv, x=\"longitude\", y=\"latitude\", layer_name=\"World Cities\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "115",
   "metadata": {},
   "source": [
    "Set the output directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "116",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.getcwd()\n",
    "out_shp = os.path.join(out_dir, \"world_cities.shp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "117",
   "metadata": {},
   "source": [
    "Convert a CSV file containing lat/long information to a shapefile."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "118",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.csv_to_shp(in_csv, out_shp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "119",
   "metadata": {},
   "source": [
    "Convert a CSV file containing lat/long information to a GeoJSON."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "120",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_geojson = os.path.join(out_dir, \"world_cities.geojson\")\n",
    "leafmap.csv_to_geojson(in_csv, out_geojson)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "121",
   "metadata": {},
   "source": [
    "Convert a CSV file containing lat/long information to a GeoPandas GeoDataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "122",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = leafmap.csv_to_gdf(in_csv)\n",
    "gdf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "123",
   "metadata": {},
   "source": [
    "### Add GeoJSON\n",
    "\n",
    "Add a GeoJSON to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "124",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[0, 0], zoom=2)\n",
    "in_geojson = (\n",
    "    \"https://github.com/opengeos/datasets/releases/download/vector/cables.geojson\"\n",
    ")\n",
    "m.add_geojson(in_geojson, layer_name=\"Cable lines\", info_mode=\"on_hover\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "125",
   "metadata": {},
   "source": [
    "Add a GeoJSON with random filled color to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "126",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[0, 0], zoom=2)\n",
    "url = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson\"\n",
    "m.add_geojson(\n",
    "    url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "127",
   "metadata": {},
   "source": [
    "Use the `style_callback` function for assigning a random color to each polygon."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "128",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "m = leafmap.Map(center=[0, 0], zoom=2)\n",
    "\n",
    "url = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson\"\n",
    "\n",
    "\n",
    "def random_color(feature):\n",
    "    return {\n",
    "        \"color\": \"black\",\n",
    "        \"fillColor\": random.choice([\"red\", \"yellow\", \"green\", \"orange\"]),\n",
    "    }\n",
    "\n",
    "\n",
    "m.add_geojson(url, layer_name=\"Countries\", style_callback=random_color)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "129",
   "metadata": {},
   "source": [
    "Use custom `style` and `hover_style` functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "130",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[0, 0], zoom=2)\n",
    "url = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson\"\n",
    "style = {\n",
    "    \"stroke\": True,\n",
    "    \"color\": \"#0000ff\",\n",
    "    \"weight\": 2,\n",
    "    \"opacity\": 1,\n",
    "    \"fill\": True,\n",
    "    \"fillColor\": \"#0000ff\",\n",
    "    \"fillOpacity\": 0.1,\n",
    "}\n",
    "hover_style = {\"fillOpacity\": 0.7}\n",
    "m.add_geojson(url, layer_name=\"Countries\", style=style, hover_style=hover_style)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "131",
   "metadata": {},
   "source": [
    "### Add shapefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "132",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[0, 0], zoom=2)\n",
    "in_shp = \"https://github.com/opengeos/leafmap/raw/master/examples/data/countries.zip\"\n",
    "m.add_shp(in_shp, layer_name=\"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "133",
   "metadata": {},
   "source": [
    "### Add KML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "134",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "135",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "in_kml = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/us_states.kml\"\n",
    "m.add_kml(in_kml, layer_name=\"US States KML\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "136",
   "metadata": {},
   "source": [
    "### Add GeoDataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "137",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "138",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "gdf = gpd.read_file(\n",
    "    \"https://github.com/opengeos/datasets/releases/download/vector/cables.geojson\"\n",
    ")\n",
    "m.add_gdf(gdf, layer_name=\"Cable lines\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "139",
   "metadata": {},
   "source": [
    "Read the GeoPandas sample dataset as a GeoDataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "140",
   "metadata": {},
   "outputs": [],
   "source": [
    "path_to_data = (\n",
    "    \"https://github.com/opengeos/datasets/releases/download/vector/nybb.geojson\"\n",
    ")\n",
    "gdf = gpd.read_file(path_to_data)\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "141",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_gdf(gdf, layer_name=\"New York boroughs\", fill_colors=[\"red\", \"green\", \"blue\"])\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "142",
   "metadata": {},
   "source": [
    "### Add point layer\n",
    "\n",
    "Add a point layer using the interactive GUI.\n",
    "\n",
    "![](https://i.imgur.com/1QVEtlN.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "143",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "144",
   "metadata": {},
   "source": [
    "Add a point layer programmatically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "145",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "url = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/us_cities.geojson\"\n",
    "m.add_point_layer(url, popup=[\"name\", \"pop_max\"], layer_name=\"US Cities\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "146",
   "metadata": {},
   "source": [
    "### Add vector\n",
    "\n",
    "The `add_vector` function supports any vector data format supported by GeoPandas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "147",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[0, 0], zoom=2)\n",
    "url = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/countries.geojson\"\n",
    "m.add_vector(\n",
    "    url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "148",
   "metadata": {},
   "source": [
    "## Download OSM data\n",
    "\n",
    "### OSM from geocode\n",
    "\n",
    "Add OSM data of place(s) by name or ID to the map. Note that the leafmap custom layer control does not support GeoJSON, we need to use the ipyleaflet built-in layer control."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "149",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "150",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(toolbar_control=False, layers_control=True)\n",
    "m.add_osm_from_geocode(\"New York City\", layer_name=\"NYC\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "151",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(toolbar_control=False, layers_control=True)\n",
    "m.add_osm_from_geocode(\"Chicago, Illinois\", layer_name=\"Chicago, IL\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "152",
   "metadata": {},
   "source": [
    "### OSM from place\n",
    "\n",
    "Add OSM entities within boundaries of geocodable place(s) to the map."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "153",
   "metadata": {},
   "source": [
    "Show OSM feature tags.\n",
    "https://wiki.openstreetmap.org/wiki/Map_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "154",
   "metadata": {},
   "outputs": [],
   "source": [
    "# leafmap.osm_tags_list()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "155",
   "metadata": {},
   "source": [
    "### OSM from address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "156",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(toolbar_control=False, layers_control=True)\n",
    "m.add_osm_from_address(\n",
    "    address=\"New York City\", tags={\"amenity\": \"bar\"}, dist=1500, layer_name=\"NYC bars\"\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "157",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(toolbar_control=False, layers_control=True)\n",
    "m.add_osm_from_address(\n",
    "    address=\"New York City\",\n",
    "    tags={\"landuse\": [\"retail\", \"commercial\"], \"building\": True},\n",
    "    dist=1000,\n",
    "    layer_name=\"NYC buildings\",\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "158",
   "metadata": {},
   "source": [
    "### OSM from bbox"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "159",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(toolbar_control=False, layers_control=True)\n",
    "north, south, east, west = 40.7551, 40.7454, -73.9738, -73.9965\n",
    "m.add_osm_from_bbox(\n",
    "    north, south, east, west, tags={\"amenity\": \"bar\"}, layer_name=\"NYC bars\"\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "160",
   "metadata": {},
   "source": [
    "### OSM from point\n",
    "\n",
    "Add OSM entities within some distance N, S, E, W of a point to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "161",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(\n",
    "    center=[46.7808, -96.0156], zoom=12, toolbar_control=False, layers_control=True\n",
    ")\n",
    "m.add_osm_from_point(\n",
    "    center_point=(46.7808, -96.0156),\n",
    "    tags={\"natural\": \"water\"},\n",
    "    dist=10000,\n",
    "    layer_name=\"Lakes\",\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "162",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(\n",
    "    center=[39.9170, 116.3908], zoom=15, toolbar_control=False, layers_control=True\n",
    ")\n",
    "m.add_osm_from_point(\n",
    "    center_point=(39.9170, 116.3908),\n",
    "    tags={\"building\": True, \"natural\": \"water\"},\n",
    "    dist=1000,\n",
    "    layer_name=\"Beijing\",\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "163",
   "metadata": {},
   "source": [
    "### OSM from view\n",
    "\n",
    "Add OSM entities within the current map view to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "164",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(toolbar_control=False, layers_control=True)\n",
    "m.set_center(-73.9854, 40.7500, 16)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "165",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.add_osm_from_view(tags={\"amenity\": \"bar\", \"building\": True}, layer_name=\"New York\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "166",
   "metadata": {},
   "source": [
    "Create a GeoPandas GeoDataFrame from place."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "167",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = leafmap.osm_gdf_from_place(\"New York City\", tags={\"amenity\": \"bar\"})\n",
    "gdf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "168",
   "metadata": {},
   "source": [
    "## Use WhiteboxTools\n",
    "\n",
    "Use the built-in toolbox to perform geospatial analysis. For example, you can perform depression filling using the sample DEM dataset downloaded in the above step.\n",
    "\n",
    "![](https://i.imgur.com/KGHly63.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "169",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import leafmap\n",
    "import urllib.request"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "170",
   "metadata": {},
   "source": [
    "Download a sample DEM dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "171",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/opengeos/whitebox-python/raw/master/whitebox/testdata/DEM.tif\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "172",
   "metadata": {},
   "outputs": [],
   "source": [
    "urllib.request.urlretrieve(url, \"dem.tif\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "173",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "174",
   "metadata": {},
   "source": [
    "Display the toolbox using the default mode."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "175",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.whiteboxgui()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "176",
   "metadata": {},
   "source": [
    "Display the toolbox using the collapsible tree mode. Note that the tree mode does not support Google Colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "177",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.whiteboxgui(tree=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "178",
   "metadata": {},
   "source": [
    "Perform geospatial analysis using the [whitebox](https://github.com/opengeos/whitebox-python) package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "179",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import whitebox"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "180",
   "metadata": {},
   "outputs": [],
   "source": [
    "wbt = whitebox.WhiteboxTools()\n",
    "wbt.verbose = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "181",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dir = os.getcwd()\n",
    "wbt.set_working_dir(data_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "182",
   "metadata": {},
   "outputs": [],
   "source": [
    "wbt.feature_preserving_smoothing(\"dem.tif\", \"smoothed.tif\", filter=9)\n",
    "wbt.breach_depressions(\"smoothed.tif\", \"breached.tif\")\n",
    "wbt.d_inf_flow_accumulation(\"breached.tif\", \"flow_accum.tif\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "183",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import imageio\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "184",
   "metadata": {},
   "outputs": [],
   "source": [
    "original = imageio.imread(os.path.join(data_dir, \"dem.tif\"))\n",
    "smoothed = imageio.imread(os.path.join(data_dir, \"smoothed.tif\"))\n",
    "breached = imageio.imread(os.path.join(data_dir, \"breached.tif\"))\n",
    "flow_accum = imageio.imread(os.path.join(data_dir, \"flow_accum.tif\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "185",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(16, 11))\n",
    "\n",
    "ax1 = fig.add_subplot(2, 2, 1)\n",
    "ax1.set_title(\"Original DEM\")\n",
    "plt.imshow(original)\n",
    "\n",
    "ax2 = fig.add_subplot(2, 2, 2)\n",
    "ax2.set_title(\"Smoothed DEM\")\n",
    "plt.imshow(smoothed)\n",
    "\n",
    "ax3 = fig.add_subplot(2, 2, 3)\n",
    "ax3.set_title(\"Breached DEM\")\n",
    "plt.imshow(breached)\n",
    "\n",
    "ax4 = fig.add_subplot(2, 2, 4)\n",
    "ax4.set_title(\"Flow Accumulation\")\n",
    "plt.imshow(flow_accum)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "186",
   "metadata": {},
   "source": [
    "## Create basemap gallery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "187",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "188",
   "metadata": {},
   "outputs": [],
   "source": [
    "for basemap in leafmap.basemaps:\n",
    "    print(basemap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "189",
   "metadata": {},
   "outputs": [],
   "source": [
    "layers = list(leafmap.basemaps.keys())[17:117]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "190",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.linked_maps(rows=20, cols=5, height=\"200px\", layers=layers, labels=layers)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "191",
   "metadata": {},
   "source": [
    "## Create linked map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "192",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "193",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.basemaps.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "194",
   "metadata": {},
   "outputs": [],
   "source": [
    "layers = [\"ROADMAP\", \"HYBRID\"]\n",
    "leafmap.linked_maps(rows=1, cols=2, height=\"400px\", layers=layers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "195",
   "metadata": {},
   "outputs": [],
   "source": [
    "layers = [\"Esri.WorldTopoMap\", \"OpenTopoMap\"]\n",
    "leafmap.linked_maps(rows=1, cols=2, height=\"400px\", layers=layers)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "196",
   "metadata": {},
   "source": [
    "Create a 2 * 2 linked map to visualize land cover change. Specify the `center` and `zoom` parameters to change the default map center and zoom level."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "197",
   "metadata": {},
   "outputs": [],
   "source": [
    "layers = [str(f\"NLCD {year} CONUS Land Cover\") for year in [2001, 2006, 2011, 2016]]\n",
    "labels = [str(f\"NLCD {year}\") for year in [2001, 2006, 2011, 2016]]\n",
    "leafmap.linked_maps(\n",
    "    rows=2,\n",
    "    cols=2,\n",
    "    height=\"300px\",\n",
    "    layers=layers,\n",
    "    labels=labels,\n",
    "    center=[36.1, -115.2],\n",
    "    zoom=9,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "198",
   "metadata": {},
   "source": [
    "## Create split-panel map\n",
    "\n",
    "Create a split-panel map by specifying the `left_layer` and `right_layer`, which can be chosen from the basemap names, or any custom XYZ tile layer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "199",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "200",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.split_map(left_layer=\"ROADMAP\", right_layer=\"HYBRID\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "201",
   "metadata": {},
   "source": [
    "Hide the zoom control from the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "202",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.split_map(\n",
    "    left_layer=\"Esri.WorldTopoMap\", right_layer=\"OpenTopoMap\", zoom_control=False\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "203",
   "metadata": {},
   "source": [
    "Add labels to the map and change the default map center and zoom level."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "204",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.split_map(\n",
    "    left_layer=\"NLCD 2001 CONUS Land Cover\",\n",
    "    right_layer=\"NLCD 2019 CONUS Land Cover\",\n",
    "    left_label=\"2001\",\n",
    "    right_label=\"2019\",\n",
    "    label_position=\"bottom\",\n",
    "    center=[36.1, -114.9],\n",
    "    zoom=10,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "205",
   "metadata": {},
   "source": [
    "## Create heat map\n",
    "\n",
    "Specify the file path to the CSV. It can either be a file locally or on the Internet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "206",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "207",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(layers_control=True)\n",
    "in_csv = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/world_cities.csv\"\n",
    "m.add_heatmap(\n",
    "    in_csv,\n",
    "    latitude=\"latitude\",\n",
    "    longitude=\"longitude\",\n",
    "    value=\"pop_max\",\n",
    "    name=\"Heat map\",\n",
    "    radius=20,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "208",
   "metadata": {},
   "source": [
    "Use the folium plotting backend."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "209",
   "metadata": {},
   "outputs": [],
   "source": [
    "from leafmap import foliumap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "210",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = foliumap.Map()\n",
    "in_csv = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/world_cities.csv\"\n",
    "m.add_heatmap(\n",
    "    in_csv,\n",
    "    latitude=\"latitude\",\n",
    "    longitude=\"longitude\",\n",
    "    value=\"pop_max\",\n",
    "    name=\"Heat map\",\n",
    "    radius=20,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "211",
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = [\"blue\", \"lime\", \"red\"]\n",
    "m.add_colorbar(colors=colors, vmin=0, vmax=10000)\n",
    "m.add_title(\"World Population Heat Map\", font_size=\"20px\", align=\"center\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "212",
   "metadata": {},
   "source": [
    "## Save map to HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "213",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "214",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_basemap(\"Esri.NatGeoWorldMap\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "215",
   "metadata": {},
   "source": [
    "Specify the output HTML file name to save the map as a web page."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "216",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.to_html(\"mymap.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "217",
   "metadata": {},
   "source": [
    "If the output HTML file name is not provided, the function will return a string containing contain the source code of the HTML file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "218",
   "metadata": {},
   "outputs": [],
   "source": [
    "html = m.to_html()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "219",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "220",
   "metadata": {},
   "source": [
    "## Use kepler plotting backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "221",
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap.kepler as leafmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "222",
   "metadata": {},
   "source": [
    "### Create an interactive map\n",
    "\n",
    "Create an interactive map. You can specify various parameters to initialize the map, such as `center`, `zoom`, `height`, and `widescreen`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "223",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[40, -100], zoom=2, height=600, widescreen=False)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "224",
   "metadata": {},
   "source": [
    "If you encounter an error saying `Error displaying widget: model not found` when trying to display the map, you can use `m.static_map()` as a workaround until this [kepler.gl bug](https://github.com/keplergl/kepler.gl/issues/1165) has been resolved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "225",
   "metadata": {},
   "outputs": [],
   "source": [
    "# m.static_map(width=1280, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "226",
   "metadata": {},
   "source": [
    "### Add CSV\n",
    "\n",
    "Add a CSV to the map. If you have a map config file, you can directly apply config to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "227",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[37.7621, -122.4143], zoom=12)\n",
    "in_csv = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/hex_data.csv\"\n",
    "config = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/hex_config.json\"\n",
    "m.add_csv(in_csv, layer_name=\"hex_data\", config=config)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "228",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.static_map(width=1280, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "229",
   "metadata": {},
   "source": [
    "### Save map config\n",
    "\n",
    "Save the map configuration as a JSON file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "230",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.save_config(\"cache/config.json\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "231",
   "metadata": {},
   "source": [
    "### Save map as html\n",
    "\n",
    "Save the map to an interactive html."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "232",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.to_html(outfile=\"cache/kepler_hex.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "233",
   "metadata": {},
   "source": [
    "### Add GeoJONS\n",
    "\n",
    "Add a GeoJSON with US state boundaries to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "234",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[50, -110], zoom=2)\n",
    "polygons = \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/us_states.json\"\n",
    "m.add_geojson(polygons, layer_name=\"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "235",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.static_map(width=1280, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "236",
   "metadata": {},
   "source": [
    "### Add shapefile\n",
    "\n",
    "Add a shapefile to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "237",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[20, 0], zoom=1)\n",
    "in_shp = \"https://github.com/opengeos/leafmap/raw/master/examples/data/countries.zip\"\n",
    "m.add_shp(in_shp, \"Countries\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "238",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.static_map(width=1280, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "239",
   "metadata": {},
   "source": [
    "### Add GeoDataFrame\n",
    "\n",
    "Add a GeoPandas GeoDataFrame to the map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "240",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "241",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = gpd.read_file(\n",
    "    \"https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/world_cities.geojson\"\n",
    ")\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "242",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[20, 0], zoom=1)\n",
    "m.add_gdf(gdf, \"World cities\")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "243",
   "metadata": {},
   "outputs": [],
   "source": [
    "# m.static_map(width=1280, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "244",
   "metadata": {},
   "source": [
    "## Use planet imagery\n",
    "\n",
    "First, you need to [sign up](https://www.planet.com/login/?mode=signup) a Planet account and get an API key. See  https://developers.planet.com/quickstart/apis.\n",
    "Uncomment the following line to pass in your API key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "245",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "246",
   "metadata": {},
   "outputs": [],
   "source": [
    "if os.environ.get(\"PLANET_API_KEY\") is None:\n",
    "    os.environ[\"PLANET_API_KEY\"] = \"your-api-key\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "247",
   "metadata": {},
   "outputs": [],
   "source": [
    "quarterly_tiles = leafmap.planet_quarterly_tiles()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "248",
   "metadata": {},
   "outputs": [],
   "source": [
    "for tile in quarterly_tiles:\n",
    "    print(tile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "249",
   "metadata": {},
   "outputs": [],
   "source": [
    "monthly_tiles = leafmap.planet_monthly_tiles()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "250",
   "metadata": {},
   "outputs": [],
   "source": [
    "for tile in monthly_tiles:\n",
    "    print(tile)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "251",
   "metadata": {},
   "source": [
    "Add a Planet monthly mosaic by specifying year and month."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "252",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_planet_by_month(year=2020, month=8)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "253",
   "metadata": {},
   "source": [
    "Add a Planet quarterly mosaic by specifying year and quarter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "254",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_planet_by_quarter(year=2019, quarter=2)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "255",
   "metadata": {},
   "source": [
    "## Use timeseries inspector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "256",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "257",
   "metadata": {},
   "outputs": [],
   "source": [
    "if os.environ.get(\"PLANET_API_KEY\") is None:\n",
    "    os.environ[\"PLANET_API_KEY\"] = \"your-api-key\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "258",
   "metadata": {},
   "outputs": [],
   "source": [
    "tiles = leafmap.planet_tiles()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "259",
   "metadata": {},
   "outputs": [],
   "source": [
    "leafmap.ts_inspector(tiles, center=[40, -100], zoom=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "260",
   "metadata": {},
   "source": [
    "## Use time slider"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "261",
   "metadata": {},
   "source": [
    "Use the time slider to visualize Planet quarterly mosaic.\n",
    "\n",
    "![](https://i.imgur.com/ipVJ4cb.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "262",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import leafmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "263",
   "metadata": {},
   "outputs": [],
   "source": [
    "if os.environ.get(\"PLANET_API_KEY\") is None:\n",
    "    os.environ[\"PLANET_API_KEY\"] = \"your-api-key\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "264",
   "metadata": {},
   "source": [
    "Specify the map center and zoom level."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "265",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(center=[38.2659, -103.2447], zoom=13)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "266",
   "metadata": {},
   "source": [
    "Use the time slider to visualize Planet quarterly mosaic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "267",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "layers_dict = leafmap.planet_quarterly_tiles()\n",
    "m.add_time_slider(layers_dict, time_interval=1)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "268",
   "metadata": {},
   "source": [
    "Use the time slider to visualize basemaps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "269",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.clear_layers()\n",
    "layers_dict = leafmap.basemap_xyz_tiles()\n",
    "m.add_time_slider(layers_dict, time_interval=1)\n",
    "m"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
